﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.CodeRush.StructuralParser;
using DevExpress.CodeRush.Core;

namespace DX_SourceOutliner
{
    public interface ISourceTreeNode
    {
        IElement Element { get; set; }
        string ElementName { get; set; }
        string IconName { get; set; }
        bool IsDocument { get; }
        string MemberVisibilityText { get; }
        string ParamterText { get; }
        string ParamterTextWithParameterNames { get; }
        string TypeDeclarationText { get; }
    }
    public class SourceTreeNode : ISourceTreeNode
    {
        private string _text;

        /// <summary>
        /// Initializes a new instance of the SourceTreeNode class.
        /// </summary>
        /// <param name="element"></param>
        public SourceTreeNode(IElement element)
        {
            Element = element;
        }

        public SourceTreeNode()
        {

        }

        public IElement Element { get; set; }

        public string ElementName
        {
            get
            {
                return Element != null ? Element.Name : _text;
            }
            set
            {
                _text = value;
            }
        }

        public string IconName { get; set; }

        public bool IsDocument
        {
            get
            {
                return Element == null;
            }
        }

        public string MemberVisibilityText
        {
            get
            {
                return GetMemberVisibilityText();
            }
        }

        public string ParamterText
        {
            get { return GetParameterText(false); }

        }

        public string ParamterTextWithParameterNames
        {
            get { return GetParameterText(true); }

        }

        public string TypeDeclarationText
        {
            get
            {
                return GetTypeDeclarationText();
            }
        }

        private string GetMemberVisibilityText()
        {
            var memberElement = Element as IMemberElement;
            return memberElement != null ? memberElement.Visibility.ToString() : string.Empty;
        }

        private string GetParameterText(bool includeParameterNamesInMethods)
        {
            var member = Element as MemberWithParameters;

            string paramString = string.Empty;

            if (member != null)
            {
                foreach (LanguageElement item in member.Parameters)
                {
                    string parameterType = parameterType = CodeRush.Language.GetSimpleTypeName(item.GetTypeName());

                    if (parameterType == string.Empty)
                        parameterType = "object";

                    if (includeParameterNamesInMethods)
                    {
                        paramString += string.Format("{0} {1}, ", parameterType, item.Name);
                    }
                    else
                        paramString += string.Format("{0}, ", parameterType);
                }
            }

            if (paramString.EndsWith(", "))
                paramString = paramString.Remove(paramString.Length - 2);
            return paramString;
        }

        private string GetTypeDeclarationText()
        {
            var element = Element as LanguageElement;

            string typeDeclaration;

            if (element != null)
                typeDeclaration = CodeRush.Language.GetSimpleTypeName(element.GetTypeName());
            else
                typeDeclaration = "object";

            return typeDeclaration;
        }

    }
}
